/**
 *
 */
package com.naiterui.ehp.bs.emr.controller.inner;

import com.naiterui.common.repository.page.PageParamVO;
import com.naiterui.common.repository.page.PageVO;
import com.naiterui.ehp.bp.constants.SwaggerTag;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.support.exception.PublicService;
import com.naiterui.ehp.bp.support.exception.ResponseVO;
import com.naiterui.ehp.bs.emr.controller.patient.EmrPatientCaseController;
import com.naiterui.ehp.bs.emr.exception.ExceptionCodes;
import com.naiterui.ehp.bs.emr.service.IPatientCaseService;
import com.naiterui.ehp.bs.emr.vo.patient.PatientCaseDetailVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;

/**
 * 患者病例信息
 *
 * @since 2.18
 */
@Validated
@Api(tags = SwaggerTag.INNER_API + SwaggerTag.MEDICAL_RECORD)
@RestController
@RequestMapping("emr/inner/patient/case")
public class EmrInnerPatientCaseController {
    private static final Logger LOGGER = LoggerFactory.getLogger(EmrPatientCaseController.class);
    @Autowired
    private IPatientCaseService patientCaseService;

    /**
     * 同步病例数据
     * InnerPatientCaseController.syncData()
     *
     * @param patientId
     * @param caseId
     *
     * @return
     *
     * @throws BusinessException
     * @Date 2018年6月1日
     * @since 2.18
     */
    @PostMapping("syncData")
    public ResponseVO<String> syncData(@NotNull(message = "患者ID不能为null") Long patientId,
                                       @NotEmpty(message = "病历ID部位能为null") String caseId)
            throws BusinessException {
        LOGGER.info("同步病例数据 请求参数：patientId = {}, caseId = {}", patientId, caseId);
        String doctorCaseId = patientCaseService.syncCaseToDoctor(patientId, caseId);
        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS, doctorCaseId);
    }

    /**
     * 增加病例医生白名单，非创建医生只设查看权限
     *
     * @param doctorId
     * @param caseId
     *
     * @return
     *
     * @throws BusinessException
     */
    @ApiOperation(value = "患者给医生发送病例", notes = "患者转发病例——增加病例可见医生白名单")
    @PostMapping("white/add")
    public ResponseVO<Void> addCaseWhite(@NotNull(message = "医生id不能为空") Long doctorId,
                                         @NotEmpty(message = "病例id不能为空") String caseId)
            throws BusinessException {
        LOGGER.info("增加医生白名单： doctorId={}, caseId={}", doctorId, caseId);
        patientCaseService.addWhite(doctorId, caseId);
        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS);
    }

    /**
     * 患者关联病例
     *
     * @param patientId
     * @param pageVO
     *
     * @return
     *
     * @throws BusinessException
     */
    @PostMapping("list")
    public ResponseVO<PageVO<PatientCaseDetailVO>> list(Long patientId, Long inquirerId,
                                                        PageParamVO pageVO) throws BusinessException {
        LOGGER.info("获取病例列表 请求参数：patientId = {}, inquirerId={}, pageVO = {}", patientId, inquirerId, pageVO);
        PageVO<PatientCaseDetailVO> patientCasePageVO = null;
        if (inquirerId == null) {
            patientCasePageVO = patientCaseService.getCaseByPatient(patientId, pageVO);
        } else {
            patientCasePageVO = patientCaseService.getCasePageList(patientId, inquirerId, pageVO);
        }
        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS, patientCasePageVO);
    }

    /**
     * 获取病例签名文件地址
     *
     * @param recordId
     *
     * @return
     *
     * @throws BusinessException
     */
    @GetMapping("signFile")
    public String caseSignPdfUrl(String recordId) throws BusinessException {
        return patientCaseService.caseSignPdfUrl(recordId);
    }
}
